<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TypeAction
 *
 * @author Administrator
 */
import('@.Filter.SecurityAction');
import('@.Service.TypeService');

class TypeAction extends SecurityAction {

    /**
     * 进入Type管理
     */
    function type() {
//        $type = D('type');
//        $parent = $_REQUEST['parent_name'];
//        $where = null;
//        if (!empty($parent)) {
//            $t = $type->where(array('name' => $parent))->find();
//            $where = array('type_id' => $t['id']);
//        } else {
//            $where = 'type_id is null';
//        }
//
//        import('ORG.Util.Page');
//
//        $count = $type->where($where)->count();
//
//        $page = new Page($count, 10);
//        if (is_array($where)) {
//            $page->parameter .= "parent_name=" . urlencode($parent) . "&";
//        }
//
//        $show = $page->show();
//
//        $list = $type->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
//
//        $this->assign('list', $list);
//        $this->assign('show', $show);


        $typeService = new TypeService();
        $typeRootList = $typeService->listRootTypes();

        $typeChildList = $typeService->listChildTypes();

        $this->assign('list', $typeRootList);

        $this->assign('childList', json_encode($typeChildList));
        $this->assign('content', 'type');
        $this->display('Admin:global');
    }

    /**
     * Type Add Page
     */
    function typeAdd() {
        $type = D('type');
        $id = $_POST['id'];
        if (!empty($id) && !is_null($p = $type->find($id))) {
            $this->assign('type', $p);

            if (!empty($p['type_id'])) {
                $list = $type->where('type_id is null')->select();

                $this->assign('list', $list);
            }

            $this->assign('content', 'typeAdd');
            $this->display('Admin:global');

            return;
        }

        $list = $type->where('type_id is null')->select();

        $this->assign('list', $list);

        $this->assign('content', 'typeAdd');
        $this->display('Admin:global');
    }

    /**
     * Type Add or Save
     */
    function typeSave() {
        $type = D('type');

        $type->startTrans();
        //Save
        $id = $_POST['id'];
        if (!empty($id)) {
            $old_type_id = $type->field('type_id')->find($id);
            $new_type_id = $_POST['type_id'];

            $type->create();
            if (false === $type->save()) {
                //更新失败
                $type->rollback();
            }
            //二级类型并且更换了父类型再进行更新
            if (!empty($new_type_id) && $old_type_id != $new_type_id) {
                $this->modifyChildId($type, $old_type_id['type_id']);
                $this->modifyChildId($type, $new_type_id);
            }

            $type->commit();

            $this->redirect('Type/type');
            return;
        }

        //Add
        $type->create();
        $lastInsId = $type->add();
        $type_id = $_POST['type_id'];
        if (0 < $lastInsId && !empty($type_id)) {
            $parent_type = $type->find($type_id);
            $parent_type['child_id'] .= $lastInsId . ',';

            $type->save($parent_type);

            $type->commit();
        } else {
            $type->rollback();
        }

        $this->redirect('Type/type');
    }

    /**
     * 类型删除
     */
    function typeDel() {

        $id = $_POST['id'];
        $type = D('type');

        $flag = true;
        if (!empty($id) && !is_null($t = $type->find($id))) {
            //类型中有子类型不能删除
            if (!empty($t['child_id'])) {
                $flag = false;
            }
            $app = D('app');
            //类型中有App不能删除
            $count = $app->where(array('type_id' => $id))->select();
            if (0 < $count) {
                $flag = false;
            }

            if ($flag) {
                //Del
                $type->where(array('id' => $id))->delete();

                $this->ajaxReturn(null, '类型删除成功', 1);
            } else {
                $this->ajaxReturn(null, '类型不能被删除', 0);
            }
        }
        $this->ajaxReturn(null, '类型不存在', 0);
    }

    /**
     * Ajax 获取当前顶级类型的子类型
     */
    function listChildType() {
        $rootId = $_POST['root_id'];
        $typeService = new TypeService();

        $childType = $typeService->listChildTypesByRootTypeId($rootId);
        $this->ajaxReturn(json_encode($childType));
    }

    /**
     * Ajax 更新二级类型
     */
    function updateChildType() {
        $rootId = $_POST['root_id'];
        $childId = $_POST['child_id'];

        $typeService = new TypeService();
        $lastId = $typeService->updateType($rootId, $childId);
        if ($lastId > 0) {
            $this->ajaxReturn(null);
        } else {
            $this->ajaxReturn(null, null, 0);
        }
    }

    /**
     * 更新下属的child id
     * @param type $id 
     */
    private function modifyChildId($type, $id) {
        $child_id = $type->where(array('type_id' => $id))->field('id')->select();
        $childIdList = '';
        foreach ($child_id as $c_id) {
            $childIdList .= $c_id['id'] . ',';
        }
        $p = $type->find($id);
        $p['child_id'] = $childIdList;

        $re = $type->save($p);
        echo $re;
    }

    function index() {
        $type = D('type');
        $s = split(',', '21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,');
//        array_d
        dump($s);
//        $a = '';
//        for ($i = 21; $i < 41; $i++) {
//            $a .= $i . ',';
//        }
//        
        $b = $type->find(1);
        $b['child_id'] = '21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,';
//        
        $type->save($b);
    }

    function findTypeAll() {
        $typeService = new TypeService();
        $this->ajaxReturn($typeService->listChildTypes());
    }

}

?>
